<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\result\Result;
use app\model\Category;
use app\model\Address as ModelAddress;
use think\facade\Request;

/**
 *
 */
class Address extends BaseController
{

    /**
     * @var string[]
     */
    protected $middleware = [
        'auth',
        'checkPermission'
    ];

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $pageNum = Request::param('pageNum');
        $pageSize = Request::param('pageSize');

        $list = ModelAddress::page($pageNum, $pageSize)->order('id', 'desc')->select();
        return Result::Success($list);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $name = Request::post('name');
        $address = Request::post('address');
        $phone = Request::post('phone');
        $uid = Request::post('uid');

        $newAddress = new ModelAddress();
        $newAddress->name = $name;
        $newAddress->address = $address;
        $newAddress->phone = $phone;
        $newAddress->uid = $uid;

        $newAddress->save();

        return Result::Success($newAddress);
    }
}
